<?php

namespace App\Admin\Controllers;

use App\RiskAddress;
use App\RiskRule;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class RiskAddressController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '危险地址';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RiskAddress());
        $grid->model()->orderByDesc('id');
        $grid->expandFilter();
        $grid->filter(function (Grid\Filter $filter) {

            $filter->disableIdFilter();

            $filter->column(1/2, function(Grid\Filter $filter){

                $filter->contains('address', __('Risk Address'));


            });
            $filter->column(1/2, function(Grid\Filter $filter){

                $filter->equal('status', __('Status'))->select(RiskAddress::STATUS_MAP);

            });
        });

        $grid->column('id', __('Id'));
        $grid->column('address', __('Risk Address'));
        $grid->column('status', '状态')->switch([
            'on'  => ['value' => 0, 'text' => '启用', 'color' => 'primary'],
            'off' => ['value' => 1, 'text' => '停用', 'color' => 'danger'],
        ]);
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RiskAddress::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('address', __('Risk Address'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RiskAddress());

        $form->text('address', __('Risk Address'));
        $form->switch('status', '状态')->states([
            'on'  => ['value' => 0, 'text' => '启用', 'color' => 'primary'],
            'off' => ['value' => 1, 'text' => '停用', 'color' => 'danger'],
        ]);

        return $form;
    }
}
